package com.financing.platform.dao.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.financing.platform.dao.IOrderDao;
import com.financing.platform.dto.OrderByGaiZaoDTO;
import com.financing.platform.dto.OrderDTO;
import com.financing.platform.dto.OrderFlowDTO;
import com.financing.platform.dto.UserInfoDTO;
import com.financing.platform.util.SqlName;
import com.financing.platform.util.StringUtil;
import com.financing.platform.vo.EmailTemplateUserVO;
import com.financing.platform.vo.OrderActivitiesVO;
import com.financing.platform.vo.OrderEmailVO;
import com.financing.platform.vo.OrderManagerUserVO;
import com.financing.platform.vo.OrderVO;

/**
 * 
* @ClassName: OrderDaoImpl 
* @Description: 订单流程处理 
* @author chenming 
* @date 2014年8月18日 下午2:41:53 
*
 */
@Repository("orderDaoImpl")
public class OrderDaoImpl extends BaseDaoImpl implements IOrderDao {

	@SuppressWarnings("unchecked")
	public List<OrderDTO> queryOrderList(OrderDTO orderDTO) {
		try {
			return (List<OrderDTO>)this.sqlMapClient.queryForList(SqlName.queryOrder, orderDTO);
		} catch (SQLException e) {
			log.error("Query order list error:",e);
			return Collections.emptyList();
		}
	}

	public OrderDTO queryOrder(OrderDTO orderDTO) {
		try {
			return (OrderDTO)this.sqlMapClient.queryForObject(SqlName.queryOrder,orderDTO);
		} catch (SQLException e) {
			log.error("Query order detail error :",e);
			return null;
		}
	}

	public int updateOrder(OrderDTO orderDTO) throws SQLException {
			return this.sqlMapClient.update(SqlName.updateOrder, orderDTO);
	}

	@SuppressWarnings("unchecked")
	public List<OrderVO> queryOrderList(OrderVO orderVo) {
		try {
			return (List<OrderVO>) this.sqlMapClient.queryForList(SqlName.queryOrderAllInfo, orderVo);
		} catch (SQLException e) {
			log.error("Query order all info error !",e);
			return Collections.emptyList();
		}
	}

	public int orderCancel(OrderDTO orderDTO) {
		try {
			return this.sqlMapClient.update(SqlName.updateOrderCancel, orderDTO);
		} catch (SQLException e) {
			log.error("Cancel error", e);
			return 0;
		}
	}

	@SuppressWarnings("unchecked")
	public List<String> queryNeedSendSMSFAId(OrderDTO orderDTO) {
		try {
			return this.sqlMapClient.queryForList(SqlName.queryNeedSendSMSFAId, orderDTO.getoProjectId());
		} catch (SQLException e) {
			log.error("Query fa id error !",e);
			return Collections.emptyList();
		}
	}

	public int addOrderFlow(OrderFlowDTO orderFlowDTO) throws SQLException {
		return (Integer) this.sqlMapClient.insert(SqlName.addOrderFlow, orderFlowDTO);
	}

	@Override
	public int queryOrderAllInfoCount(OrderVO orderVo) throws SQLException {
		return (Integer) this.sqlMapClient.queryForObject(SqlName.queryOrderAllInfoCount,orderVo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OrderActivitiesVO> queryOrderForUserId(String userId) throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryOrderForUserId, userId);
	}

	@Override
	public int updateOrderDefriendStatus(OrderDTO orderDTO) throws SQLException {
		return this.sqlMapClient.update(SqlName.updateOrderDefriendStatus, orderDTO);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Integer> queryOrderFormProjectId(OrderDTO orderDTO) throws Exception {
		return this.sqlMapClient.queryForList(SqlName.queryOrderFormProjectId, orderDTO.getoProjectId());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EmailTemplateUserVO> queryFaOrderPerForEmail()
			throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryFaOrderPerForEmail);
	}

	@Override
	public List<HashMap<String, String>> queryOrderByProIdList(OrderVO orderDTO) throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryOrderByProId, orderDTO);
	}

	@Override
	public int countOrderByProIdList(OrderVO orderDTO) throws SQLException {
		return (Integer) this.sqlMapClient.queryForObject(SqlName.countOrderByProId, orderDTO);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<OrderDTO> queryInfoByOrderIds(String ids) throws SQLException{
		//为空或者包含' 不需要处理
		ids=StringUtil.splitToIn(ids);
		return this.sqlMapClient.queryForList(SqlName.queryInfoByOrderIds,ids);
	}
	

	@SuppressWarnings("unchecked")
	@Override
	public List<OrderEmailVO> queryneedSendEmailToManager() throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryneedSendEmailToManager);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OrderEmailVO> queryneedSendEmailToManagerUser()
			throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryneedSendEmailToManagerUser);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OrderEmailVO> queryneedSendEmailToManagerUserDetail(UserInfoDTO user)
			throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryneedSendEmailToManagerUserDetail, user);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OrderEmailVO> queryneedSendEmailToManagerSM()
			throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryneedSendEmailToManagerSM);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OrderEmailVO> queryneedSendEmailToManagerUserSM()
			throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryneedSendEmailToManagerUserSM);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OrderEmailVO> queryneedSendEmailToManagerUserDetailSM(UserInfoDTO user)
			throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryneedSendEmailToManagerUserDetailSM, user);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> queryOrderManagerUserBySource(int sourceCode) throws SQLException{
		return this.sqlMapClient.queryForList(SqlName.queryOrderManagerUserBySource, sourceCode);
	}
	
	@Override
	public int queryOrderFollowUpAllInfoCount(OrderVO orderVo) throws SQLException {
		return (Integer) this.sqlMapClient.queryForObject(SqlName.queryOrderFollowUpAllInfoCount,orderVo);
	}

	@Override
	public List<OrderVO> queryOrderFollowUpList(OrderVO orderVo) throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryOrderFollowUpList,orderVo);
	}

	@Override
	public List<OrderEmailVO> queryPevcneedSendEmailToManager()
			throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryPevcneedSendEmailToManager);
	}

	@Override
	public List<OrderEmailVO> queryPEVCneedSendEmailToManagerUser()
			throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryPEVCneedSendEmailToManagerUser);
	}

	@Override
	public List<OrderEmailVO> queryPevcneedSendEmailToManagerUserDetail(
			String uid) throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryPevcneedSendEmailToManagerUserDetail,uid);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<OrderByGaiZaoDTO> queryOrderByGaiZao(
			OrderByGaiZaoDTO orderByGaiZaoDTO) throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryOrderByGaiZao,orderByGaiZaoDTO);
	}

	@Override
	public int queryOrderCountByGaiZao(OrderByGaiZaoDTO orderByGaiZaoDTO)
			throws SQLException {
		return (Integer) this.sqlMapClient.queryForObject(SqlName.queryOrderCountByGaiZao,orderByGaiZaoDTO);
	}

	@Override
	public int updateOrderByGaiZao(OrderByGaiZaoDTO orderByGaiZaoDTO)
			throws SQLException {
		return this.sqlMapClient.update(SqlName.updateOrderByGaiZao, orderByGaiZaoDTO);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<OrderByGaiZaoDTO> queryOrderBaseInfoByGaiZao(
			OrderByGaiZaoDTO orderByGaiZaoDTO) throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryOrderBaseInfoByGaiZao,orderByGaiZaoDTO);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<OrderByGaiZaoDTO> queryOrderListByGaiZao(
			OrderByGaiZaoDTO orderByGaiZaoDTO) throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryOrderListByGaiZao,orderByGaiZaoDTO);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<OrderManagerUserVO> queryOrderOwnerUserList() throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryOrderOwnerUserList);
	}

	@Override
	public List<OrderFlowDTO> queryOrderFlowList(String orderId) throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryOrderFlowList,orderId);
	}

	@Override
	public void updateOrderFlow(OrderFlowDTO orderFlowDTO) throws SQLException {
		 this.sqlMapClient.update(SqlName.updateOrderFlow,orderFlowDTO);
	}

	@Override
	public Map<String, Object> queryOrderMaxMinDateByProductId(Integer integer) throws SQLException {
		return (Map<String, Object>)this.sqlMapClient.queryForObject("order.queryOrderMaxMinDateByProductId",integer);
	}

    @Override
	public BigDecimal getAllAmount(OrderDTO orderDTO) throws SQLException {
		// TODO Auto-generated method stub
		return (BigDecimal) this.sqlMapClient.queryForObject("order.getAllAmount", orderDTO);
	}
  
	@Override
	public List<String> queryFaUserOrder() throws SQLException {
		return this.sqlMapClient.queryForList("order.queryFaUserOrder");
	}

	@Override
	public List<Map<String, Object>> queryOrderOwnerUserCountAmount() throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryOrderOwnerUserCountAmount);
	}
}